<!DOCTYPE html>
<html>
<head>
    <title>角色管理</title>
    <#include "/header.html">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <#if shiro.hasPermission("sys:role:save")>
                <button class="layui-btn layui-btn-normal" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i> 添加 </button>
                </#if>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <#if shiro.hasPermission("sys:role:update")>
            <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            </#if>
            <#if shiro.hasPermission("sys:role:delete")>
            <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </#if>
        </script>

    </div>
</div>
<script type="text/javascript">
    var formDataId = 0;
    var menuTreeData;
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        $(document).ready(function () {
            $.ajax({
                type: "POST",
                url: serverUrl('sys/menu/tree'),
                data: {},
                dataType: "json",
                success: function (result) {
                    menuTreeData = result;
                }
            });
        });

        table.render({
            elem: '#currentTableId',
            url: serverUrl('sys/role/list'),
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
            cols: [[
                {type: "numbers", width: 80},
                {field: 'roleName', minWidth: 120, title: '角色名称'},
                {field: 'remark', title: '备注', minWidth: 150},
                {field: 'createTime', minWidth: 100, title: '创建时间'},
                {title: '操作', width: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [15, 30, 50],
            limit: 15,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                formDataId = 0;
                openFormView('sys/role/form.html', '添加角色');

            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                formDataId = data.roleId;
                openFormView('sys/role/form.html', '编辑角色');
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除 '+data.roleName+' 吗？', function (index) {
                    deleteTableRow('sys/role/delete', [data.roleId], function (rs) {
                        obj.del();
                    });
                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>